﻿using NPOI.SS.Formula.Functions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Zocono.WCS.Domain.PlcBytesObject
{
    public class PlanarGeneralWriteWcsActionFx : WriteByteObjectBase
    {
        public PlanarGeneralWriteWcsActionFx() { }
        public List<DxInfo> Infos { get; set; } = new List<DxInfo>();
        public PlanarGeneralWriteWcsActionFx(long plcConnectionID
            , long plcWriteDbID
            , int dbCode
            , int dbStart
            , string objectType)
        {
            PlcConnectionID = plcConnectionID;
            PlcWriteDbID = plcWriteDbID;
            DbCode = dbCode;
            DbStart = dbStart;
            ObjectType = objectType;
        }

        

        public override byte[] Deserialize()
        {
            List<DxInfo> data = new List<DxInfo>() {
                new DxInfo { code = "ITCESHI1",type = 2,res = 5 },
                new DxInfo { code = "ITCESHI2",type = 2,res = 4 },
                new DxInfo { code = "ITCESHI3",type = 2,res = 3 },
                new DxInfo { code = "ITCESHI4",type = 2,res = 3 },
                new DxInfo { code = "ITCESHI5",type = 2,res = 2 },
                new DxInfo { code = "ITCESHI6",type = 2,res = 2 },
                new DxInfo { code = "ITCESHI7",type = 2,res = 2 },
                new DxInfo { code = "ITCESHI8",type = 2,res = 2 },
                new DxInfo { code = "ITCESHI9",type = 2,res = 2 },
                new DxInfo { code = "ITCESHI10",type = 2,res = 1 },
                new DxInfo { code = "ITCESHI11",type = 2,res = 1 },
                new DxInfo { code = "ITCESHI12",type = 2,res = 1 },
                new DxInfo { code = "ITCESHI13",type = 2,res = 1 },
                new DxInfo { code = "ITCESHI14",type = 2,res = 1 },
                new DxInfo { code = "ITCESHI15",type = 2,res = 3 },
                new DxInfo { code = "ITCESHI16",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI17",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI18",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI19",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI20",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI21",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI22",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI23",type = 2,res = 0 },
                new DxInfo { code = "ITCESHI24",type = 2,res = 0 }
            };
            byte[] bytes = new byte[936];
            //第一个位置只写电芯类型
            HelperByte.SetByteAt(bytes, 0, 0);
            HelperByte.SetByteAt(bytes, 1, 0);
            HelperByte.SetCharsAt(bytes, 2 , "");
            HelperByte.SetIntAt(bytes, 32, Infos[0].type);
            HelperByte.SetByteAt(bytes, 34, 0);
            foreach (var item in Infos)
            {
                //写入预留长度 1200CPL必须写
                HelperByte.SetByteAt(bytes, (item.location ) * 36, 30);
                byte lengthItem = Convert.ToByte(item.code.Length);
                HelperByte.SetByteAt(bytes, (item.location ) * 36 + 1, lengthItem);
                HelperByte.SetCharsAt(bytes, (item.location) * 36 + 2 , item.code);
                HelperByte.SetIntAt(bytes, (item.location) * 36 + 32, item.type);
                HelperByte.SetByteAt(bytes, (item.location) * 36 + 34, item.res);
                
            }  
            
            return bytes;
        }

    }
    public class DxInfo
    {
        /// <summary>
        /// 电芯位置
        /// </summary>
        public int location { get; set; }
        /// <summary>
        /// 电芯代码
        /// </summary>
        public string code { get; set; }
        /// <summary>
        /// 电芯类型
        /// </summary>
        public short type { get; set; }
        /// <summary>
        /// 分档结果
        /// </summary>
        public byte res { get; set; }

    }
}
